<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="租金" prop="rent">
        <el-input
          v-model="queryParams.rent"
          placeholder="请输入租金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人才类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择人才类型" clearable>
          <el-option
            v-for="dict in dict.type.elite_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租赁申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择租赁申请状态" clearable>
          <el-option
            v-for="dict in dict.type.approval_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租赁时间" prop="applicationDate">
        <el-date-picker
          clearable
          v-model="queryParams.applicationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择租赁时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="住户数量" prop="households">
        <el-input
          v-model="queryParams.households"
          placeholder="请输入住户数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:applications:export']"
        >
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="applicationsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="applicationId" />
      <el-table-column label="人才姓名" align="center" prop="nickName" />
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="公寓地址" align="center" prop="address" />
      <el-table-column label="人才类型" align="center" prop="eliteType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.elite_type" :value="scope.row.eliteType" />
        </template>
      </el-table-column>
      <el-table-column label="原租金" align="center" prop="rent" />
      <el-table-column label="人才优惠租金" align="center" prop="discountRent" />
      <el-table-column label="租赁申请状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="住户数量" align="center" prop="households" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <div class="u-line-2" :title="scope.row.remark">{{ scope.row.remark }}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="审批备注" align="center" prop="advice">
        <template slot-scope="scope">
          <div class="u-line-2" :title="scope.row.advice">{{ scope.row.advice }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['system:applications:edit']"
          >
            查看
          </el-button>
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 3)"
            v-hasPermi="['system:applications:edit']"
          >
            审批
          </el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:applications:remove']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租赁申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="人才姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入人才姓名" disabled />
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系方式" disabled />
        </el-form-item>
        <el-form-item label="公寓地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入租金" disabled />
        </el-form-item>
        <el-form-item label="原租金" prop="rent">
          <el-input v-model="form.rent" placeholder="请输入租金" disabled />
        </el-form-item>
        <el-form-item label="人才优惠租金" prop="discountRent">
          <el-input v-model="form.discountRent" placeholder="请输入租金" disabled />
        </el-form-item>
        <el-form-item label="人才类型" prop="eliteType">
          <el-select v-model="form.eliteType" placeholder="请选择人才类型" disabled>
            <el-option
              v-for="dict in dict.type.elite_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否通过" prop="approvalStatus" v-if="openType != 2">
          <el-select
            v-model="form.approvalStatus"
            placeholder="请选择是否通过"
            :disabled="openType == 2"
          >
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁申请状态" prop="" v-else>
          <el-select v-model="form.status" placeholder="请选择是否通过" disabled>
            <el-option
              v-for="dict in dict.type.approval_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="租赁时间" prop="applicationDate">
          <el-date-picker
            clearable
            v-model="form.applicationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择租赁时间"
            disabled
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item label="住户数量" prop="households">
          <el-input v-model="form.households" placeholder="请输入住户数量" disabled />
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
        <el-form-item label="审批备注" prop="advice">
          <el-input v-model="form.advice" placeholder="请输入审批备注" :disabled="openType == 2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="openType != 2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApplications,
  getApplications,
  delApplications,
  addApplications,
  updateApplications,
  approvalApplications
} from '@/api/system/applications'

export default {
  name: 'Applications',
  dicts: ['approval_status', 'elite_type', 'sys_yes_no'],
  data() {
    return {
      openType: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁申请表格数据
      applicationsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        buildingId: null,
        apartmentId: null,
        rent: null,
        userType: null,
        phone: null,
        status: null,
        applicationDate: null,
        households: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        approvalStatus: [{ required: true, message: '请选择是否通过', trigger: 'change' }],
        advice: [{ required: true, message: '请输入审批备注', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询租赁申请列表 */
    getList() {
      this.loading = true
      listApplications(this.queryParams).then((response) => {
        this.applicationsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        applicationId: null,
        houseId: null,
        buildingId: null,
        apartmentId: null,
        rent: null,
        createBy: null,
        userType: null,
        phone: null,
        status: null,
        applicationDate: null,
        households: null,
        createTime: null,
        updateTime: null,
        remark: null,
        advice: null,
        approvalStatus: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.applicationId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加租赁申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row, openType) {
      this.openType = openType
      this.reset()
      const applicationId = row.applicationId || this.ids
      getApplications(applicationId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = openType == 2 ? '查看租赁申请' : '修改租赁申请'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const params = {
            applicationId: this.form.applicationId,
            approvalStatus: this.form.approvalStatus,
            advice: this.form.advice
          }
          if (this.form.applicationId != null) {
            approvalApplications(params).then((response) => {
              this.$modal.msgSuccess('审批成功')
              this.open = false
              this.getList()
            })
          } else {
            addApplications(params).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applicationIds = row.applicationId || this.ids
      this.$modal
        .confirm('是否确认删除租赁申请编号为"' + applicationIds + '"的数据项？')
        .then(function () {
          return delApplications(applicationIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'room/applications/export',
        {
          ...this.queryParams
        },
        `applications_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
